/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PRESENT;

import UTILS.ErrorMessages;
import UTILS.GUIConstant;

/**
 *
 * @author HuuTri
 */
public class MatHangGUI extends javax.swing.JFrame {

    /**
     * Creates new form QuanGUI
     */
    public MatHangGUI() {
        initComponents();
        initCustomOption();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtMaLoaiMatHang_Error = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtTenMatHang_Error = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtSoLuongTon_Error = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtMoTa = new javax.swing.JTextArea();
        txtTenMatHang = new javax.swing.JTextField();
        txtMaLoaiMatHang = new javax.swing.JComboBox();
        txtMaMatHang = new javax.swing.JTextField();
        txtSoLuongTon = new javax.swing.JTextField();
        btnThemMoi = new javax.swing.JButton();
        btnLuuLai = new javax.swing.JButton();
        btnXoaBo = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTable1 = new org.jdesktop.swingx.JXTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(800, 600));

        jSplitPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jSplitPane1.setDividerLocation(400);
        jSplitPane1.setDividerSize(10);
        jSplitPane1.setAutoscrolls(true);
        jSplitPane1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jSplitPane1.setMinimumSize(new java.awt.Dimension(0, 0));
        jSplitPane1.setPreferredSize(new java.awt.Dimension(0, 0));

        jPanel1.setForeground(new java.awt.Color(255, 51, 51));
        jPanel1.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        jLabel1.setText("Mã Mặt Hàng:");

        txtMaLoaiMatHang_Error.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N
        txtMaLoaiMatHang_Error.setForeground(new java.awt.Color(255, 51, 51));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        jLabel2.setText("Tên Mặt Hàng:");

        txtTenMatHang_Error.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N
        txtTenMatHang_Error.setForeground(new java.awt.Color(255, 51, 51));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        jLabel3.setText("Mô tả: ");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        jLabel4.setText("Số Lượng Tồn:");
        jLabel4.setToolTipText("");

        txtSoLuongTon_Error.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N
        txtSoLuongTon_Error.setForeground(new java.awt.Color(255, 51, 51));

        txtMoTa.setColumns(20);
        txtMoTa.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        txtMoTa.setLineWrap(true);
        txtMoTa.setRows(5);
        txtMoTa.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        jScrollPane3.setViewportView(txtMoTa);

        txtTenMatHang.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        txtTenMatHang.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        txtTenMatHang.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtTenMatHangFocusLost(evt);
            }
        });

        txtMaLoaiMatHang.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        txtMaLoaiMatHang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        txtMaLoaiMatHang.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        txtMaLoaiMatHang.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtMaLoaiMatHangFocusLost(evt);
            }
        });

        txtMaMatHang.setEditable(false);
        txtMaMatHang.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        txtMaMatHang.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        txtMaMatHang.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtMaMatHangKeyTyped(evt);
            }
        });

        txtSoLuongTon.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        txtSoLuongTon.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        txtSoLuongTon.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtSoLuongTonFocusLost(evt);
            }
        });
        txtSoLuongTon.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtSoLuongTonKeyTyped(evt);
            }
        });

        btnThemMoi.setText("Thêm Mới");
        btnThemMoi.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnThemMoiMouseClicked(evt);
            }
        });

        btnLuuLai.setText("Lưu Lại");

        btnXoaBo.setText("Xóa Bỏ");
        btnXoaBo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnXoaBoMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtSoLuongTon_Error, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtMaLoaiMatHang, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtMaMatHang))
                    .addComponent(txtSoLuongTon)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(txtTenMatHang_Error, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtMaLoaiMatHang_Error, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(txtTenMatHang)))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnThemMoi)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnLuuLai)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnXoaBo)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(8, 8, 8)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMaLoaiMatHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtMaMatHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtTenMatHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtTenMatHang_Error)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtMaLoaiMatHang_Error)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSoLuongTon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSoLuongTon_Error)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThemMoi)
                    .addComponent(btnLuuLai)
                    .addComponent(btnXoaBo))
                .addContainerGap())
        );

        jSplitPane1.setLeftComponent(jPanel1);

        jXTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jXTable1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE)
        );

        jSplitPane1.setRightComponent(jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 563, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //Hàm khởi tạo các giá trị đặc biệt
    private void initCustomOption() {
        setSuspendedState();
    }
    
    private void setSuspendedState() {
        setEnabledState(false);
        btnLuuLai.setEnabled(false);
        btnXoaBo.setEnabled(false);
        btnThemMoi.setEnabled(true);
    }
    
    private void setCreationState() {
        setEnabledState(true);
        btnLuuLai.setEnabled(true);
        btnXoaBo.setEnabled(false);
        btnThemMoi.setEnabled(true);
    }
    
    private void setSelectionState() {
        setEnabledState(true);
        btnLuuLai.setEnabled(true);
        btnThemMoi.setEnabled(true);
        btnXoaBo.setEnabled(true);
    }
    
    private void setEnabledState(boolean flag) {
        txtMaLoaiMatHang.setEnabled(flag);
        txtMaMatHang.setEnabled(flag);
        txtSoLuongTon.setEnabled(flag);
        txtTenMatHang.setEnabled(flag);
        txtMoTa.setEnabled(flag);
    }
    
    private void txtMaLoaiMatHangFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtMaLoaiMatHangFocusLost
        if (txtMaLoaiMatHang.getSelectedIndex() < 0) {
            //Trường hợp index là -1
            txtMaLoaiMatHang_Error.setText(ErrorMessages.MATHANGGUI_TXTMALOAIMATHANG_ERROR_NONE);
        } else {
            txtMaLoaiMatHang_Error.setText("");
        }
    }//GEN-LAST:event_txtMaLoaiMatHangFocusLost

    private void txtTenMatHangFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtTenMatHangFocusLost
        if (txtTenMatHang.getText().isEmpty()) {
            //Trường hợp tên mặt hàng bị bỏ trống
            txtTenMatHang_Error.setText(ErrorMessages.MATHANGGUI_TXTTENMATHANG_ERROR_NONE);
        } else if (txtTenMatHang.getText().length() < GUIConstant.MATHANG_TEN_LENGHT_MIN) {
            //Trường hợp tên mặt hàng quá ngắn
            txtTenMatHang_Error.setText(ErrorMessages.MATHANGGUI_TXTTENMATHANG_ERROR_LESS + GUIConstant.MATHANG_TEN_LENGHT_MIN);
        } else if (txtTenMatHang.getText().length() > GUIConstant.MATHANG_TEN_LENGHT_MAX) {
            //Trường hợp tên mặt hàng quá dài
            txtTenMatHang_Error.setText(ErrorMessages.MATHANGGUI_TXTTENMATHANG_ERROR_GREATER_THAN +
                    GUIConstant.MATHANG_TEN_LENGHT_MAX);
        } else {
            txtTenMatHang_Error.setText("");
        }        
    }//GEN-LAST:event_txtTenMatHangFocusLost

    private void txtSoLuongTonFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtSoLuongTonFocusLost
        if (txtSoLuongTon.getText().isEmpty()) {
            //Trường hợp số lượng tồn bị bỏ trống
            txtSoLuongTon_Error.setText(ErrorMessages.MATHANGGUI_TXTSOLUONGTON_ERROR_NONE);
        } else if (txtSoLuongTon.getText().length() > GUIConstant.MATHANG_SOLUONGTON_LENGTH_MAX) {
            //Trường hợp số lượng tồn quá lớn
            txtSoLuongTon_Error.setText(ErrorMessages.MATHANGGUI_TXTSOLUONGTON_ERROR_GREATER_THAN +
                    GUIConstant.MATHANG_SOLUONGTON_MAX);
        } else if (Integer.parseInt(txtSoLuongTon.getText()) < GUIConstant.MATHANG_SOLUONGTON_MIN) {
            //Trường hợp số lượng tồn không có
            txtSoLuongTon_Error.setText(ErrorMessages.MATHANGGUI_TXTSOLUONGTON_ERROR_LESS_THAN + 
                    GUIConstant.MATHANG_SOLUONGTON_MIN);
        } else {
            txtSoLuongTon_Error.setText("");
        }
    }//GEN-LAST:event_txtSoLuongTonFocusLost

    private void txtSoLuongTonKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSoLuongTonKeyTyped
        if (!Character.isDigit(evt.getKeyChar())) {
            evt.consume();
        }
    }//GEN-LAST:event_txtSoLuongTonKeyTyped

    private void txtMaMatHangKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMaMatHangKeyTyped
        if (!Character.isDigit(evt.getKeyChar())) {
            evt.consume();
        }
    }//GEN-LAST:event_txtMaMatHangKeyTyped

    private void btnThemMoiMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnThemMoiMouseClicked
        setCreationState();
    }//GEN-LAST:event_btnThemMoiMouseClicked

    private void btnXoaBoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnXoaBoMouseClicked
        setSelectionState();
    }//GEN-LAST:event_btnXoaBoMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MatHangGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MatHangGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MatHangGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MatHangGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MatHangGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLuuLai;
    private javax.swing.JButton btnThemMoi;
    private javax.swing.JButton btnXoaBo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSplitPane jSplitPane1;
    private org.jdesktop.swingx.JXTable jXTable1;
    private javax.swing.JComboBox txtMaLoaiMatHang;
    private javax.swing.JLabel txtMaLoaiMatHang_Error;
    private javax.swing.JTextField txtMaMatHang;
    private javax.swing.JTextArea txtMoTa;
    private javax.swing.JTextField txtSoLuongTon;
    private javax.swing.JLabel txtSoLuongTon_Error;
    private javax.swing.JTextField txtTenMatHang;
    private javax.swing.JLabel txtTenMatHang_Error;
    // End of variables declaration//GEN-END:variables
}
